Back to course home
0% completed
Vote For New Content
Meta Hard Problems
Sliding Window Median (hard)
Solution: Sliding Window Median
Alien Dictionary (hard)
Solution: Alien Dictionary
Minimum Window Substring (hard)
Solution: Minimum Window Substring
Best Meeting Point (hard)
Solution: Best Meeting Point
Maximum Sum of 3 Non-Overlapping Subarrays (hard)
Solution: Maximum Sum of 3 Non-Overlapping Subarrays
Making A Large Island (hard)
Solution: Making A Large Island
Basic Calculator (Hard)
Solution: Basic Calculator
Serialize and Deserialize Binary Tree (hard)
Solution: Serialize and Deserialize Binary Tree
Longest Increasing Path in a Matrix (hard)
Solution: Longest Increasing Path in a Matrix
Valid Palindrome III (hard)
Solution: Valid Palindrome III
Maximum Swap (medium)
Problem Statement
Given a non-negative integer num, return the maximum number, which you can create by swapping any two digits of the number only once. If no swaps can improve the number, return the original number.
Examples
-
Example 1:
- Input:
2736 - Expected Output:
7236 - Justification: Swapping the first and second digits (
2and7) results in the largest possible number.
- Input:
-
Example 2:
- Input:
9965 - Expected Output:
9965 - Justification: The number is already in its maximum form, so no swap is needed.
- Input:
-
Example 3:
- Input:
7281912 - Expected Output:
9281712 - Justification: Swapping the first digit (
7) with the first9found from the left results in the maximum number.
- Input:
Try it yourself
Try solving this question here:
Java
. . . .
Solution: Shortest Path in Binary Matrix
Solution: Maximum Swap
Mark as Completed
On this page
Problem Statement
Examples
Try it yourself